<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>历史试卷</title>
  <!--  引入核心皮肤-->
  <link rel="stylesheet" href="../../layui/css/layui.css">
  <!--   引入核心js-->
  <script src="../../layui/layui.js"></script>
  <script >
    layui.use(['layer','jquery','table','form'],function () {
      let layer=layui.layer;//定义组件
      let $=layui.jquery;//定义组件
      let table =layui.table;//定义表格组件
      let form=layui.form;//定义表格组件
      table.render({
        elem:'#dg',
        url:'../../question/selectAll',
        page:true,//开启分页
        parseData:function (res) {
          return{
            "code":0,
            "msg":"",
            "count":1000,
            "data":res
          }
        },
        cols: [[ //表头
          {field: 'questionid', title: 'ID',type:'checkbox'}
          ,{field: 'questionname', title: '试卷名称'}
          // ,{field: 'startdate', title: '开始日期'}
          // ,{field: 'enddate', title: '结束日期'}
          ,{field: 'majorid', title: '试卷分类'}
          ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}

        ]],
        done: function(res, curr, count){
          $(".layui-table-box").find("[data-field='id']").css("display","none");

          $("[data-field='majorid']").children().each(function(){
            if($(this).text()=='601'){
              $(this).text("计算机类")
            }else if($(this).text()=='603'){
              $(this).text("计算机类")
            }else if($(this).text()=='602'){
              $(this).text("食品类")
            }else if($(this).text()=='604'){
              $(this).text("食品类")
            }else if($(this).text()=='605'){
              $(this).text("生活类")
            }
          });
          pageCurr=curr;
        }
      });
      table.on('tool(dg)', function(obj){
        var data = obj.data;
        if(obj.event === 'yl'){
          layer.open({
            title: '预览试卷',
            type: 2,
            content: "myhquestion.html",
            area: ['800px','800px'],
            success: function (layero, index) {
              // 获取子页面的iframe
              var iframe = window['layui-layer-iframe' + index];
              // 向子页面的全局函数child传参
              iframe.child(data);
            }
          });
        } else if(obj.event === 'del'){
          $.post('../../question/delete',{
            questionId: data.questionid
          },function (flag) {
            console.log(data)
            if(flag){
              layer.msg('删除数据成功',{icon:6});
              table.reload('dg');
            }else{
              layer.msg('删除数据失败',{icon:6});
            }
          })
        }
      });
    })

  </script>
  <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="yl">预览</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </script>
</head>
<body>
<table id="dg" lay-filter="dg"></table>
</body>
</html>